# @Version        : 1.0
# @Update Time    : 2025/3/14 16:36
# @File           : notification_view.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息

import flet as ft

class NotificationView(ft.Column):
    def __init__(self, page: ft.Page, params: dict = None):
        super().__init__()
        self.page = page
        self.params = params
        self.switch_states = {
            "auto_update": True,
            "stock_notice": False,
            "discount_offer": False,
            "daily_promotion": False
        }
        self.build_interface()

    def build_interface(self):
        # 顶部导航栏
        app_bar = ft.Container(
            content=ft.Row([
                ft.Text("通知设置", size=20, weight=ft.FontWeight.BOLD)
            ], alignment=ft.MainAxisAlignment.START),
            padding=15,
            border=ft.border.only(bottom=ft.border.BorderSide(1, ft.colors.GREY_300))
        )

        # 设置项列表
        settings_list = ft.Column(
            controls=[
                self._build_setting_item(ft.icons.UPDATE, "自动更新", "auto_update"),
                ft.Divider(height=1, color=ft.colors.GREY_200),
                self._build_setting_item(ft.icons.INVENTORY_2, "库存通知", "stock_notice"),
                ft.Divider(height=1, color=ft.colors.GREY_200),
                self._build_setting_item(ft.icons.LOCAL_OFFER, "折扣优惠", "discount_offer"),
                ft.Divider(height=1, color=ft.colors.GREY_200),
                self._build_setting_item(ft.icons.CAMPAIGN, "每日促销", "daily_promotion")
            ],
            spacing=0
        )

        # 底部保存按钮
        save_button = ft.Container(
            content=ft.ElevatedButton(
                "保存设置",
                height=45,
                style=ft.ButtonStyle(
                    bgcolor=ft.colors.GREEN_500,
                    shape=ft.RoundedRectangleBorder(radius=8),
                    overlay_color=ft.colors.GREEN_600
                ),
                on_click=self.save_settings
            ),
            padding=ft.padding.symmetric(horizontal=20, vertical=10),
            bgcolor=ft.colors.WHITE,
            alignment=ft.alignment.center  # 添加这一行以居中对齐按钮
        )

        self.controls = [
            ft.Column(
                controls=[app_bar, ft.Container(settings_list, expand=True)],
                expand=True
            ),
            save_button
        ]

    def _build_setting_item(self, icon, title, key):
        return ft.Container(
            content=ft.Row([
                ft.Icon(icon, color=ft.colors.GREY_700),
                ft.Text(title, size=16, expand=True),
                ft.Switch(
                    value=self.switch_states[key],
                    active_color=ft.colors.GREEN_500,
                    inactive_thumb_color=ft.colors.GREY_400,
                    on_change=lambda e, k=key: self.switch_states.update({k: e.control.value})
                )
            ], spacing=15),
            padding=20,
            bgcolor=ft.colors.WHITE,
            on_click=lambda e: print(f"{title} clicked")
        )

    def save_settings(self, e):
        print("保存设置:", self.switch_states)
        # 实际保存逻辑
        self.page.snack_bar = ft.SnackBar(
            ft.Text("设置已保存！"),
            bgcolor=ft.colors.GREEN_500
        )
        self.page.snack_bar.open = True
        self.page.update()
